// ----------------------------------
// RSDK Project: Sonic 1 Forever X Doki Doki Literature Club
// Script Description: Warning
// Script Author: KarlEmerald
// ----------------------------------

//-------Aliases-------//

private alias object.value0 : object.timer
private alias object.value1 : object.LetterTimer
private alias object.value2 : object.currentLine
private alias object.value3 : object.NumberOfLetters
private alias object.value4 : object.currentColor
private alias object.value5 : object.LetterSpeed
private alias object.value6 : object.skip
private alias object.value7 : object.ArrowX
private alias object.value8 : object.ArrowTimer
private alias object.value9 : object.charSkipper
private alias object.value10 : object.sfxCheck
private alias object.value11 : object.charIndex
private alias object.value12 : object.drawPos.x
private alias object.value13 : object.drawPos.y
private alias object.value14 : object.letterID
private alias object.value15 : object.touchDelay

reserve function ConsoleDraw
reserve function LoadMenuScreenHandler

private value Arrow_Direction = 0;
private value StopOtherActions = 0;
private value DDLC_MainSheet = 0;
private value DDLC_CharSheet = 0;

private table MenuControl_RandomCharacters
	5 // Space
	0 // @
	4 // . and !
	1 // tiny randos
	2 // brack
end table

private table MenuControl_LetterWidthNumbers
	10, 6, 9, 9, 9, 9, 9, 9, 9, 9
end table

private table MenuControl_TinyLetterWidth
	4, 3, 4, 4, 3, 3, 4, 4, 3, 3, 3, 3, 5, 4, 4, 3, 5, 3, 3, 3, 4, 5, 5, 3, 3, 3 // upper
	4, 3, 3, 3, 3, 2, 3, 3, 1, 3, 3, 1, 5, 3, 3, 3, 4, 2, 3, 3, 3, 3, 5, 3, 3, 3 // lower
	3, 3, 3, 3, 3, 3, 3, 3, 3, 3 // numbers
end table

private table MenuControl_LetterWidth
	10, 9, 10, 10, 8, 8, 11, 9, 4, 9, 10, 7, 12, 10, 11, 9, 11, 9, 9, 9, 10, 10, 15, 9, 9, 9, 
	9, 9, 9, 8, 9, 9, 8, 8, 8, 5, 7, 8, 6, 11, 8, 8, 8, 8, 7, 7, 7, 9, 7, 11, 8, 9, 7
end table

// Function declarations

function LoadMenuScreenHandler
	if FirstBootWarning == 1
		CheckEqual(MenuIntroSkip, 1)
		temp0 = checkResult
		CheckEqual(saved.beeToggle, 1)	
		temp0 |= checkResult
		if temp0 == 1
			ResetObjectEntity(arrayPos0, TypeName[Opening], 0, object.xpos, object.ypos)
			DisableDrawRealQuickBuddy = false
		else
			ResetObjectEntity(arrayPos0, TypeName[Splash], 0, object.xpos, object.ypos)
		end if	
	end if	
end function


event ObjectMain

	if object.currentLine < 2
		temp0 = 256
		temp5 = screen.xcenter
		temp2 = temp0
		temp2 /= 2
		temp5 -= temp2
		temp6 = temp5
		temp6 += temp0

		temp1 = 210
		temp1 -= 29
		temp3 = temp1
		temp3 += 57

		CheckTouchRect(temp5, temp1, temp6, temp3)
		if checkResult > -1
			if object.touchDelay == 0
				inputPress.start = true
				object.touchDelay = 10
			end if	
		end if

		if object.touchDelay > 0
			object.touchDelay--
		end if
	end if	

	// I'm just like Jamesith when he was working on old Conquest
	if inputPress.buttonA == 1
		inputPress.start = 1
	end if

	switch object.state
	case 0
		SetClassicFade(color7, color7, color7, 255)
		object.timer = 256
		object.alpha = 256

		LoadSpriteSheet("DDLCMenu/TextBox.gif")
		DDLC_MainSheet = object.spriteSheet 

		LoadSpriteSheet("DDLCMenu/Portraits.gif")
		DDLC_CharSheet = object.spriteSheet 

		object.state++
		break
	case 1	
		if object.timer > 0
			object.timer -= 3
		else
			object.state++
			object.timer = 0
			object.currentLine = 0
			object.NumberOfLetters = 0
			object.LetterTimer = 0
			object.LetterSpeed = 2
			object.skip = 0
			object.charSkipper = 0
		end if
		SetClassicFade(color7, color7, color7, object.timer)
		break
	case 2	
		object.timer++
		if object.timer == 40

			object[+1].type = TypeName[Text]
			object[+1].priority = PRIORITY_ACTIVE	
			object[+1].propertyValue = 0 // Text Handler 	
			object[+1].ixpos = screen.xcenter
			object[+1].ixpos -= 120
			object[+1].value11 = object[+1].ixpos
			object[+1].iypos = 190
			object[+1].value10 = object[+1].iypos

			object.timer = 0
			object.state++
		end if
		break
	case 3
		GetTextInfo(temp0, 22, TEXTINFO_TEXTSIZE, object.currentLine, 0)
		if object.NumberOfLetters < temp0
			object.LetterTimer++
			if object.LetterTimer == object.LetterSpeed
				object.LetterTimer = 0
				object.NumberOfLetters++	
					
				//if object.NumberOfLetters > 4
				//	PlaySfx(SfxName[Score Add], 0)
				//end if	
			end if
			if inputPress.start == 1
				object.NumberOfLetters = temp0
				object.skip = 1
			end if
		else
			object.state++
		end if	
		break	
	case 4	
		if inputPress.start == 1
			if object.currentLine == 2
				PlaySfx(SfxName[Select], false)
				object.state = 5
				object.currentLine++

				//Reset the text everytime always.
				foreach(TypeName[Text], arrayPos0, ALL_ENTITIES)
					object[arrayPos0].type = TypeName[Blank object]
				next
			else
				object.NumberOfLetters = 0
				object.LetterTimer = 0
				object.LetterSpeed = 3
				object.currentLine++
				object.skip = 0
				object.sfxCheck = 0

				//Reset the text everytime always.
				foreach(TypeName[Text], arrayPos0, ALL_ENTITIES)
					if object[arrayPos0].propertyValue > 0
						object[arrayPos0].type = TypeName[Blank object]
					end if	
				next

				object.state--
			end if	
		end if

		if object.currentLine == 2
			temp0 = 140
			temp1 = 14

			temp2 = temp0
			temp2 /= 2
			FlipSign(temp2)	
			temp2 += screen.xcenter
			temp3 = temp1
			temp3 /= 2
			FlipSign(temp3)
			temp3 += 90

			temp4 = temp2
			temp4 += temp0

			temp5 = temp3
			temp5 += temp1

			CheckTouchRect(temp2, temp3, temp4, temp5)
			if checkResult > -1
				PlaySfx(SfxName[Select], false)
				object.state = 5
				object.currentLine++

				//Reset the text everytime always.
				foreach(TypeName[Text], arrayPos0, ALL_ENTITIES)
					object[arrayPos0].type = TypeName[Blank object]
				next
			end if
		end if

		if stage.debugMode == 1
			if inputPress.buttonY == 1
				object.NumberOfLetters = 0
				object.LetterTimer = 0
				object.LetterSpeed = 3
				object.skip = 0
				object.sfxCheck = 0
				//Reset the text everytime always.
				foreach(TypeName[Text], arrayPos0, ALL_ENTITIES)
					if object[arrayPos0].propertyValue > 0
						object[arrayPos0].type = TypeName[Blank Object]
					end if	
				next

				object.state--
			end if
		end if
		break
	case 5	
		if object.value0 < 220
			object.value0++
		else
			DisableDrawRealQuickBuddy = true
			object.state++
		end if	

		if object.alpha > 0
			object.value16++
			if object.value16 == 2
				object.value16 = 0
				object.alpha--
			end if	
		else
			object.alpha = 0
		end if
		SetPaletteFade(4, 0, 4, object.alpha, 0, 256)
		break
	case 6	
		arrayPos0 = object.entityPos
		CallFunction(LoadMenuScreenHandler)
		FirstBootWarning = 1
		WriteSaveRAM()
		break
	end switch

	object.drawOrder = 6
end event


event ObjectDraw
	if DisableDrawRealQuickBuddy == false
		SetActivePalette(4, 0, 256)

		object.spriteSheet = DDLC_CharSheet

		temp0 = 35
		temp2 = 110

		temp1 = screen.xcenter
		temp1 -= temp0
		DrawSpriteScreenXY(139, temp1, screen.ycenter)
		temp1 = screen.xcenter
		temp1 -= temp2
		object.direction = FLIP_X
		DrawSpriteScreenFX(138, FX_FLIP, temp1, screen.ycenter)
		temp1 = screen.xcenter
		temp1 += temp2
		DrawSpriteScreenXY(141, temp1, screen.ycenter)
		temp1 = screen.xcenter
		temp1 += temp0
		DrawSpriteScreenXY(140, temp1, screen.ycenter)

		object.spriteSheet = DDLC_MainSheet
		SetActivePalette(0, 0, 256)
		if object.currentLine < 3
			if object.state >= 3
				DrawSpriteScreenXY(0, screen.xcenter, 210)

				if object.currentLine == 2
					temp0 = 140
					temp1 = 14

					temp2 = temp0
					temp2 /= 2
					FlipSign(temp2)	
					temp2 += screen.xcenter
					temp3 = temp1
					temp3 /= 2
					FlipSign(temp3)
					temp3 += 90
					DrawRect(temp2, temp3, temp0, temp1, color7, color5, color6, 255)
					temp2++	
					temp3++
					temp0 -= 2	
					temp1 -= 2
					DrawRect(temp2, temp3, temp0, temp1, color7, color5, color6, 255)	
					DrawRect(temp2, temp3, temp0, temp1, color7, color7, color7, 180)		

					DrawSpriteScreenXY(133, screen.xcenter, temp3)		
					temp3 += 6
					temp0 = screen.xcenter
					temp0 += 56
					DrawSpriteScreenXY(136, temp0, temp3)		
				else
					if object.state == 4
						temp0 = screen.xcenter
						temp0 += 120
						
						temp0 += object.ArrowX

						switch Arrow_Direction 
						case 0
							if object.ArrowX <= 2
								if object.ArrowTimer == 5
									object.ArrowTimer = 0
									object.ArrowX++
								else
									object.ArrowTimer++
								end if
							else
								object.ArrowTimer = 0
								Arrow_Direction++
							end if
							break
						case 1
							if object.ArrowX => 0
								if object.ArrowTimer == 5
									object.ArrowTimer = 0
									object.ArrowX--
								else
									object.ArrowTimer++
								end if
							else
								object.ArrowTimer = 0
								Arrow_Direction--
							end if
							break			
						end switch	
						
						DrawSpriteScreenXY(2, temp0, 230)	
					end if		
				end if
			end if
		end if
	end if	
end event


event ObjectStartup
	LoadSpriteSheet("DDLCMenu/TextBox.gif")
	LoadPalette("Warning.act", 4, 0, 0, 95)
	

	SpriteFrame(-128, -29, 256, 57, 1, 57)
	SpriteFrame(-128, -29, 256, 20, 1, 37) // name tag
	SpriteFrame(-3, -4, 5, 7, 225, 29) // arrow


	//Uppercase Tiny Font | 3-28
	temp0 = 0
	temp2 = 1
	temp3 = 242
	while temp0 < 2
		temp1 = 0
		while temp1 < 13
			SpriteFrame(0, 0, 5, 5, temp2, temp3) 
			temp1++
			temp2 += 5
		loop
		temp2 = 1
		temp0++
		temp3 += 6
	loop

	//Lowercase Tiny Font | 29-54
	temp0 = 0
	temp2 = 1
	temp3 = 254
	while temp0 < 2
		temp1 = 0
		while temp1 < 13
			SpriteFrame(0, 0, 5, 7, temp2, temp3) 
			temp1++
			temp2 += 5
		loop
		temp2 = 1
		temp0++
		temp3 += 7
	loop

	SpriteFrame(0, 0, 15, 12, 286, 255)  // 55 . small font
	SpriteFrame(0, 0, 15, 12, 302, 255)  // 56 ! small font
	SpriteFrame(0, 0, 1, 6, 52, 269) // 57 . 
	SpriteFrame(0, 0, 1, 6, 54, 269) // 58 , 
	SpriteFrame(0, 0, 1, 6, 56, 269) // 59 ' 
	SpriteFrame(0, 0, 1, 6, 58, 269) // 60 ! 	
	SpriteFrame(0, 0, 0, 0, 0, 0) // 61

	// tiny numbers 62-72
	temp1 = 0
	temp2 = 1
	while temp1 < 10
		SpriteFrame(0, 0, 5, 5, temp2, 269) 
		temp1++
		temp2 += 5
	loop

	SpriteFrame(0, 0, 3, 5, 67, 263) // 69 >
	SpriteFrame(0, 0, 2, 5, 74, 263) // 70 [
	SpriteFrame(0, 0, 2, 5, 71, 263) // 71 ]
	SpriteFrame(0, 0, 3, 5, 68, 269) // 72 _
	SpriteFrame(0, 0, 2, 5, 75, 269) // 73 (
	SpriteFrame(0, 0, 2, 5, 72, 269) // 74 )	
	SpriteFrame(0, 0, 3, 5, 67, 257) // 75 = 

	SpriteFrame(-160, -60, 320, 120, 78, 172) // 76 		

	//Uppercase
	temp0 = 0
	temp2 = 1
	temp3 = 115
	while temp0 < 2
		temp1 = 0
		while temp1 < 13
			SpriteFrame(0, 0, 15, 12, temp2, temp3) //1-26
			temp1++
			temp2 += 16
		loop
		temp2 = 1
		temp0++
		temp3 += 13
	loop

	SpriteFrame(-8, 0, 4, 4, 236, 116) //27

	//Lowercase
	temp0 = 0
	temp2 = 1
	temp3 = 141
	while temp0 < 2
		temp1 = 0
		while temp1 < 13
			SpriteFrame(0, 0, 11, 14, temp2, temp3) //27-53
			temp1++
			temp2 += 12
		loop
		temp2 = 1
		temp0++
		temp3 += 15
	loop

	SpriteFrame(-14, 3, 27, 7, 1, 234) // 133
	SpriteFrame(0, 0, 426, 240, 0, 0) // 134
	SpriteFrame(0, 0, 426, 240, 0, 240) // 135
	SpriteFrame(-8, -8, 16, 16, 29, 225) // 136
	SpriteFrame(-8, -8, 16, 16, 231, 20) // 137
	SpriteFrame(-49, -74, 97, 148, 70, 1) //139 Sayori	
	SpriteFrame(-46, -83, 92, 166, 168, 1) //141 Yuri		
	SpriteFrame(-42, -80, 103, 159, 261, 1)  //138 Monika
	SpriteFrame(-34, -65, 68, 130, 1, 1) //140 Natsuki	

	switch saved.graphics
		case 0
			color1 = 32
			color2 = 64
			color3 = 96
			color4 = 128
			color5 = 160
			color6 = 192
			color7 = 224
			break
		case 1
			color1 = 36
			color2 = 72
			color3 = 108
			color4 = 144
			color5 = 180
			color6 = 216
			color7 = 252
			break
		case 2
			color1 = 52
			color2 = 87
			color3 = 116
			color4 = 144
			color5 = 172
			color6 = 206
			color7 = 255
			break			
	end switch

	foreach(TypeName[Warning], arrayPos0, ALL_ENTITIES)
		CallFunction(LoadMenuScreenHandler)
	next
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
